using System;
using System.Runtime.InteropServices;

using Scenario.WorkflowEngine.DataModel.WorkflowProcesses;
using Scenario.WorkflowEngine.DataModel.WorkflowRelevantData;
using System.Text;
using Scenario.WorkflowEngine;

namespace Scenario.WAPI
{
	/// <summary>
	/// Descrizione di riepilogo per wrpWAPIProcess.
	/// </summary>
	public partial class ProcessHelper
	{
        public ProcessHelper() {}

        #region Start Workflow, Workflow instance
        /// <summary>
        /// Creates new process, set process attributes and start in synch/no synch way
        /// </summary>
        /// <param name="sWorkflow">Workflow name to start</param>
        /// <param name="xmlAttributes">Workflow attributes to set</param>
        /// <param name="bSynch">Start mode</param>
        /// <returns>Process Id</returns>
        public String StartWorkflow(string sWorkflow, XMLAttributes xmlAttributes, bool bSynch)
        {
            StringBuilder processId = new StringBuilder(100);
            WAPI.Response wapiResponse = new Scenario.WAPI.Response();
            WAPI.ProcessHelper.CreateProcessInstance(sWorkflow, processId, ref wapiResponse);

            if (wapiResponse.iCode != 0)
                throw new EngineException(wapiResponse.sMsg);

            if (xmlAttributes != null)
            {
                XMLAttributes xmlAttributesNotSetted = SetAttributes(processId.ToString(), xmlAttributes);
            }

            if (bSynch)
                WAPI.ProcessHelper.StartProcessInstanceInSynchWay(processId.ToString(), ref wapiResponse);
            else
                WAPI.ProcessHelper.StartProcessInstance(processId.ToString(), ref wapiResponse);

            if (wapiResponse.iCode != 0)
                throw new EngineException(wapiResponse.sMsg);

            return processId.ToString();
        }


        public XMLWorkflow CreateWorkflowInstance(String workflowName)
        {
            WAPI.Response wapiResponse = new WAPI.Response();
            StringBuilder processId = new StringBuilder(100);
            string sWorkflow = WAPI.ProcessHelper.CreateProcessInstance(workflowName, processId, ref wapiResponse);

            if (wapiResponse.iCode != 0)
            {
                throw new Exception.WAPIException(wapiResponse.iCode, wapiResponse.sMsg);
            }

            XMLWorkflow xmlWorkflow = new XMLWorkflow();
            xmlWorkflow.Parse(sWorkflow);

            return xmlWorkflow;
        }

        /// <summary>
        /// Starts the process instance.
        /// </summary>
        /// <param name="processId">process ID.</param>
        public void StartWorkflowInstance(String processID, XMLAttributes xmlAttributes, bool bSynch)
        {
            WAPI.Response wapiResponse = new WAPI.Response();

            if (xmlAttributes != null)
            {
                XMLAttributes xmlAttributesNotSetted = SetAttributes(processID, xmlAttributes);
            }

            if (bSynch)
            {
                WAPI.ProcessHelper.StartProcessInstanceInSynchWay(processID, ref wapiResponse);
            }
            else
            {
                WAPI.ProcessHelper.StartProcessInstance(processID, ref wapiResponse);
            }

            if (wapiResponse.iCode != 0)
                throw new Exception.WAPIException(wapiResponse.iCode, wapiResponse.sMsg);
        }
        #endregion

        #region Manage Workflow instance
        public XMLWorkflow GetWorkflowInstance(String processID)
        {
            WAPI.Response wapiResponse = new WAPI.Response();
            StringBuilder processId = new StringBuilder(100);
            string sWorkflow = WAPI.ProcessHelper.GetProcessInstance(processID, ref wapiResponse);

            if (wapiResponse.iCode != 0)
            {
                throw new Exception.WAPIException(wapiResponse.iCode, wapiResponse.sMsg);
            }

            XMLWorkflow xmlWorkflow = new XMLWorkflow();
            xmlWorkflow.Parse(sWorkflow);

            return xmlWorkflow;
        }

        public XMLWorkflows GetWorkflowInstances()
        {
            WAPI.Response wapiResponse = new WAPI.Response();
            StringBuilder processId = new StringBuilder(100);
            string sWorkflow = WAPI.ProcessHelper.GetProcesses(ref wapiResponse);

            if (wapiResponse.iCode != 0)
            {
                throw new Exception.WAPIException(wapiResponse.iCode, wapiResponse.sMsg);
            }

            XMLWorkflows xmlWorkflows = new XMLWorkflows();
            xmlWorkflows.Parse(sWorkflow);

            return xmlWorkflows;
        }

        public XMLWorkflows GetWorkflowsTemplate()
        {
            Response wapiResponse = new Response();
            String sXML = WAPI.ProcessHelper.GetWorkflowList(ref wapiResponse);

            if (wapiResponse.iCode != 0)
                throw new Exception.WAPIException(wapiResponse.iCode, wapiResponse.sMsg);

            XMLWorkflows xmlWorkflows = new XMLWorkflows();
            xmlWorkflows.Parse(sXML);

            return xmlWorkflows;

        }

        #endregion

        #region Workflow attrbitutes
        public XMLAttributes SetAttributes(String processID, XMLAttributes xmlAttributes)
        {
            if (xmlAttributes.Count == 0)
                return null;
            Response response = new Response();

            String sResult = String.Empty;
            String sXML = xmlAttributes.GetXML(false, 0);
            //TODO:
			sXML = sXML.Replace ("\\", "\\\\");
            sResult = ProcessHelper.SetProcessInstanceAttributes(processID, sXML,ref response);

            if (response.iCode != 0)
                throw new Exception.WAPIException(response.iCode, response.sMsg);

            XMLAttributes xmlAttributesNotSetted = new XMLAttributes();
            xmlAttributesNotSetted.Parse(sResult);

            return xmlAttributesNotSetted;
        }

        public XMLAttributes GetAttributes(String processId)
        {
            Response response = new Response();

            String sResult = String.Empty;
            sResult = ProcessHelper.GetProcessInstanceAttributes(processId, ref response);

            if (response.iCode != 0)
                throw new Exception.WAPIException(response.iCode, response.sMsg);

            XMLAttributes xmlAttributes = new XMLAttributes();
            xmlAttributes.Parse(sResult);

            return xmlAttributes;
        }
        #endregion

        #region Workflow reports
        public XMLReports GetReports(String processID)
        {
            Response response = new Response();
            String sResult = String.Empty;

            sResult = ProcessHelper.GetReports(processID, ref response);

            if (response.iCode != 0)
                throw new Exception.WAPIException(response);

            XMLReports xmlReports = new XMLReports();
            xmlReports.Parse(sResult);

            return xmlReports;
        }
        #endregion
    }
}
